﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using DSREF = CPPEI.Coolzon.ServiceProxy.DesignServiceRef;

namespace CPPEI.Coolzon.Designer.Event.NCL
{
    /// <summary>
    /// 事件处理器
    /// </summary>
    class EventProcesser
    {
        private static object s_lockObj = new object();
        public static EventProcesser s_instance;
        public static EventProcesser Instance
        {
            get
            {
                if (s_instance == null)
                {
                    lock (s_lockObj)
                    {
                        if (s_instance == null)
                        {
                            s_instance = new EventProcesser();
                        }
                    }
                }
                return s_instance;
            }
        }

        #region Login
        [Coolzon.Common.Subscribe]
        public void OnReceiveBeginLoginEvent(BeginLoginEvent @event)
        {
            EndLoginEvent @endEvent = null;
            DSREF.DesignServiceClient client = Coolzon.ServiceProxy.DesignServiceProxyManager.Create();
            try
            {
                DSREF.LoginRequest request = new DSREF.LoginRequest();
                request.UserAccount = @event.Account;
                request.Password = @event.Password;
                DSREF.SingleResultOf_User result = client.Login(request);
                if (result.IsSuccess)
                {
                    @endEvent = new EndLoginEvent(NCLEventState.Finished, result.Data);
                }
                else
                {
                    @endEvent = new EndLoginEvent(NCLEventState.Failure, null);
                }
            }
            catch
            {
                @endEvent = new EndLoginEvent(NCLEventState.Failure, null);
            }
            NCLEventBus.Publish(@endEvent);
        }
        #endregion

        #region create workspace
        [Coolzon.Common.Subscribe]
        public void OnReceiveBeginCreateWorkspaceEvent(BeginCreateWorkspaceEvent @event)
        {
            EndCreateWorkspaceEvent @endEvent = null;
            DSREF.DesignServiceClient client = Coolzon.ServiceProxy.DesignServiceProxyManager.Create();
            try
            {
                DSREF.CreateWorkspaceRequest request = new DSREF.CreateWorkspaceRequest(@event.Computer, @event.LocalPath, @event.Token);
                DSREF.SingleResultOf_Workspace result = client.CreateWorkspace(request);
                if (result.IsSuccess)
                {
                    @endEvent = new EndCreateWorkspaceEvent(NCLEventState.Finished, result.Data);
                }
                else
                {
                    @endEvent = new EndCreateWorkspaceEvent(NCLEventState.Failure, null);
                }
            }
            catch
            {
                @endEvent = new EndCreateWorkspaceEvent(NCLEventState.Failure, null);
            }
            NCLEventBus.Publish(@endEvent);
        }
        #endregion

        #region get workspace list
        [Coolzon.Common.Subscribe]
        public void OnReceiveBeginGetWorkspaceListEvent(BeginGetWorkspaceListEvent @event)
        {
            EndGetWorkspaceListEvent @endEvent = null;
            DSREF.DesignServiceClient client = Coolzon.ServiceProxy.DesignServiceProxyManager.Create();
            try
            {
                DSREF.GetWorkspaceListRequest request = new DSREF.GetWorkspaceListRequest(@event.Computer, @event.Token);
                DSREF.ListResultOf_Workspace result = client.GetWorkspaceList(request);
                if (result.IsSuccess)
                {
                    @endEvent = new EndGetWorkspaceListEvent(NCLEventState.Finished, result.Data);
                }
                else
                {
                    @endEvent = new EndGetWorkspaceListEvent(NCLEventState.Failure, null);
                }
            }
            catch
            {
                @endEvent = new EndGetWorkspaceListEvent(NCLEventState.Failure, null);
            }
            NCLEventBus.Publish(@endEvent);
        }
        #endregion

        #region create solution
        [Coolzon.Common.Subscribe]
        public void OnReceiveBeginCreateSolutionEvent(BeginCreateSolutionEvent @event)
        {
            EndCreateSolutionEvent @endEvent = null;
            DSREF.DesignServiceClient client = Coolzon.ServiceProxy.DesignServiceProxyManager.Create();
            try
            {
                DSREF.CreateSolutionRequest request = new DSREF.CreateSolutionRequest(@event.Caption, @event.Name, @event.RTVersion, @event.Token, @event.Type);
                DSREF.SingleResultOf_Solution result = client.CreateSolution(request);
                if (result.IsSuccess)
                {
                    @endEvent = new EndCreateSolutionEvent(NCLEventState.Finished, result.Data);
                }
                else
                {
                    @endEvent = new EndCreateSolutionEvent(NCLEventState.Failure, null);
                }
            }
            catch
            {
                @endEvent = new EndCreateSolutionEvent(NCLEventState.Failure, null);
            }
            NCLEventBus.Publish(@endEvent);
        }
        #endregion

        #region get solution list
        [Coolzon.Common.Subscribe]
        public void OnReceiveBeginGetSolutionListEvent(BeginGetSolutionListEvent @event)
        {
            EndGetSolutionListEvent @endEvent = null;
            DSREF.DesignServiceClient client = Coolzon.ServiceProxy.DesignServiceProxyManager.Create();
            try
            {
                DSREF.GetSolutionListRequest request = new DSREF.GetSolutionListRequest(@event.Token, Guid.Empty);
                DSREF.ListResultOf_Solution result = client.GetSolutionList(request);
                if (result.IsSuccess)
                {
                    @endEvent = new EndGetSolutionListEvent(NCLEventState.Finished, result.Data);
                }
                else
                {
                    @endEvent = new EndGetSolutionListEvent(NCLEventState.Failure, null);
                }
            }
            catch
            {
                @endEvent = new EndGetSolutionListEvent(NCLEventState.Failure, null);
            }
            NCLEventBus.Publish(@endEvent);
        }
        #endregion

        #region Create DataSource
        [Coolzon.Common.Subscribe]
        public void OnReceiveBeginCreateDataSourceEvent(BeginCreateDataSourceEvent @event)
        {
            EndCreateDataSourceEvent @endEvent = null;
            DSREF.DesignServiceClient client = Coolzon.ServiceProxy.DesignServiceProxyManager.Create();
            try
            {
                DSREF.CreateDataSourceRequest request = new DSREF.CreateDataSourceRequest(@event.DataSource, @event.Token);
                DSREF.ServiceResult result = client.CreateDataSource(request);
                if (result.IsSuccess)
                {
                    @endEvent = new EndCreateDataSourceEvent(NCLEventState.Finished, @event.DataSource);
                }
                else
                {
                    @endEvent = new EndCreateDataSourceEvent(NCLEventState.Failure, null);
                }
            }
            catch
            {
                @endEvent = new EndCreateDataSourceEvent(NCLEventState.Failure, null);
            }
            NCLEventBus.Publish(@endEvent);
        }
        #endregion

        #region Create DataSet
        [Coolzon.Common.Subscribe]
        public void OnReceiveBeginCreateDataSetEvent(BeginCreateDataSetEvent @event)
        {
            EndCreateDataSetEvent @endEvent = null;
            DSREF.DesignServiceClient client = Coolzon.ServiceProxy.DesignServiceProxyManager.Create();
            try
            {
                DSREF.CreateDataSetRequest request = new DSREF.CreateDataSetRequest(@event.DataSet, @event.DataSourceId, @event.Parameters, @event.Token);
                DSREF.ServiceResult result = client.CreateDataSet(request);
                if (result.IsSuccess)
                {
                    @endEvent = new EndCreateDataSetEvent(NCLEventState.Finished, @event.DataSourceId, @event.DataSet, @event.Parameters);
                }
                else
                {
                    @endEvent = new EndCreateDataSetEvent(NCLEventState.Failure, @event.DataSourceId, null, null);
                }
            }
            catch
            {
                @endEvent = new EndCreateDataSetEvent(NCLEventState.Failure, @event.DataSourceId, null, null);
            }
            NCLEventBus.Publish(@endEvent);
        }
        #endregion

        #region Update DataSource
        [Coolzon.Common.Subscribe]
        public void OnReceiveBeginCreateDataSourceEvent(BeginUpdateDataSourceEvent @event)
        {
            EndUpdateDataSourceEvent @endEvent = null;
            DSREF.DesignServiceClient client = Coolzon.ServiceProxy.DesignServiceProxyManager.Create();
            try
            {
                DSREF.CreateDataSourceRequest request = new DSREF.CreateDataSourceRequest(@event.DataSource, @event.Token);
                DSREF.ServiceResult result = client.UpdateDataSource(request);
                if (result.IsSuccess)
                {
                    @endEvent = new EndUpdateDataSourceEvent(NCLEventState.Finished, @event.DataSource);
                }
                else
                {
                    @endEvent = new EndUpdateDataSourceEvent(NCLEventState.Failure, null);
                }
            }
            catch
            {
                @endEvent = new EndUpdateDataSourceEvent(NCLEventState.Failure, null);
            }
            NCLEventBus.Publish(@endEvent);
        }
        #endregion

        #region Update DataSet
        [Coolzon.Common.Subscribe]
        public void OnReceiveBeginUpdateDataSetEvent(BeginUpdateDataSetEvent @event)
        {
            EndUpdateDataSetEvent @endEvent = null;
            DSREF.DesignServiceClient client = Coolzon.ServiceProxy.DesignServiceProxyManager.Create();
            try
            {
                DSREF.CreateDataSetRequest request = new DSREF.CreateDataSetRequest(@event.DataSet, @event.DataSourceId, @event.Parameters, @event.Token);
                DSREF.ServiceResult result = client.UpdateDataSet(request);
                if (result.IsSuccess)
                {
                    @endEvent = new EndUpdateDataSetEvent(NCLEventState.Finished, @event.DataSourceId, @event.DataSet, @event.Parameters);
                }
                else
                {
                    @endEvent = new EndUpdateDataSetEvent(NCLEventState.Failure, @event.DataSourceId, null, null);
                }
            }
            catch
            {
                @endEvent = new EndUpdateDataSetEvent(NCLEventState.Failure, @event.DataSourceId, null, null);
            }
            NCLEventBus.Publish(@endEvent);
        }
        #endregion

        #region Delete DataSource
        [Coolzon.Common.Subscribe]
        public void OnReceiveBeginDeleteDataSourceEvent(BeginDeleteDataSourceEvent @event)
        {
            EndDeleteDataSourceEvent @endEvent = null;
            DSREF.DesignServiceClient client = Coolzon.ServiceProxy.DesignServiceProxyManager.Create();
            try
            {
                DSREF.DeleteDataSourceRequest request = new DSREF.DeleteDataSourceRequest(@event.DataSourceId, @event.Token);
                DSREF.ServiceResult result = client.DeleteDataSource(request);
                if (result.IsSuccess)
                {
                    @endEvent = new EndDeleteDataSourceEvent(NCLEventState.Finished, @event.DataSourceId);
                }
                else
                {
                    @endEvent = new EndDeleteDataSourceEvent(NCLEventState.Failure, @event.DataSourceId);
                }
            }
            catch
            {
                @endEvent = new EndDeleteDataSourceEvent(NCLEventState.Failure, @event.DataSourceId);
            }
            NCLEventBus.Publish(@endEvent);
        }
        #endregion

        #region Delete DataSet
        [Coolzon.Common.Subscribe]
        public void OnReceiveBeginDeleteDataSetEvent(BeginDeleteDataSetEvent @event)
        {
            EndDeleteDataSetEvent @endEvent = null;
            DSREF.DesignServiceClient client = Coolzon.ServiceProxy.DesignServiceProxyManager.Create();
            try
            {
                DSREF.DeleteDataSetRequest request = new DSREF.DeleteDataSetRequest(@event.DataSetId, @event.DataSourceId, @event.Token);
                DSREF.ServiceResult result = client.DeleteDataSet(request);
                if (result.IsSuccess)
                {
                    @endEvent = new EndDeleteDataSetEvent(NCLEventState.Finished, @event.DataSourceId, @event.DataSetId);
                }
                else
                {
                    @endEvent = new EndDeleteDataSetEvent(NCLEventState.Failure, @event.DataSourceId, @event.DataSetId);
                }
            }
            catch
            {
                @endEvent = new EndDeleteDataSetEvent(NCLEventState.Failure, @event.DataSourceId, @event.DataSetId);
            }
            NCLEventBus.Publish(@endEvent);
        }
        #endregion

        #region Get DataSource
        [Coolzon.Common.Subscribe]
        public void OnReceiveBeginGetDataSourceListEvent(BeginGetDataSourceListEvent @event)
        {
            EndGetDataSourceListEvent @endEvent = null;
            DSREF.DesignServiceClient client = Coolzon.ServiceProxy.DesignServiceProxyManager.Create();
            try
            {
                DSREF.GetDataSourceListRequest request = new DSREF.GetDataSourceListRequest(@event.SolutionId, @event.Token);
                DSREF.ListResultOf_DataSource result = client.GetDataSourceList(request);
                if (result.IsSuccess)
                {
                    @endEvent = new EndGetDataSourceListEvent(NCLEventState.Finished, result.Data);
                }
                else
                {
                    @endEvent = new EndGetDataSourceListEvent(NCLEventState.Failure, null);
                }
            }
            catch
            {
                @endEvent = new EndGetDataSourceListEvent(NCLEventState.Failure, null);
            }
            NCLEventBus.Publish(@endEvent);
        }
        #endregion

        #region Get DataSet
        [Coolzon.Common.Subscribe]
        public void OnReceiveBeginGetDataSetListEvent(BeginGetDataSetListEvent @event)
        {
            EndGetDataSetListEvent @endEvent = null;
            DSREF.DesignServiceClient client = Coolzon.ServiceProxy.DesignServiceProxyManager.Create();
            try
            {
                DSREF.GetDataSetListRequest request = new DSREF.GetDataSetListRequest(@event.DataSourceId, @event.Token);
                DSREF.ListResultOf_DataSet result = client.GetDataSetList(request);
                if (result.IsSuccess)
                {
                    @endEvent = new EndGetDataSetListEvent(NCLEventState.Finished, result.Data);
                }
                else
                {
                    @endEvent = new EndGetDataSetListEvent(NCLEventState.Failure, null);
                }
            }
            catch
            {
                @endEvent = new EndGetDataSetListEvent(NCLEventState.Failure, null);
            }
            NCLEventBus.Publish(@endEvent);
        }
        #endregion


        #region Publish DataSource
        [Coolzon.Common.Subscribe]
        public void OnReceiveBeginPublishDataSourceEvent(BeginPublishDataSourceEvent @event)
        {
            EndPublishDataSourceEvent @endEvent = null;
            DSREF.DesignServiceClient client = Coolzon.ServiceProxy.DesignServiceProxyManager.Create();
            try
            {
                DSREF.PublishDataSourceRequest request = new DSREF.PublishDataSourceRequest(@event.DataSourceId, @event.SolutionId, @event.Token);
                DSREF.ServiceResult result = client.PublishDataSource(request);
                if (result.IsSuccess)
                {
                    @endEvent = new EndPublishDataSourceEvent(NCLEventState.Finished, @event.SolutionId, @event.DataSourceId);
                }
                else
                {
                    @endEvent = new EndPublishDataSourceEvent(NCLEventState.Failure, @event.SolutionId, @event.DataSourceId);
                }
            }
            catch
            {
                @endEvent = new EndPublishDataSourceEvent(NCLEventState.Failure, @event.SolutionId, @event.DataSourceId);
            }
            NCLEventBus.Publish(@endEvent);
        }
        #endregion

        #region Publish DataSource
        [Coolzon.Common.Subscribe]
        public void OnReceiveBeginGetPublishedDataSourceListEvent(BeginGetPublishedDataSourceListEvent @event)
        {
            EndGetPublishedSolutionListEvent @endEvent = null;
            DSREF.DesignServiceClient client = Coolzon.ServiceProxy.DesignServiceProxyManager.Create();
            try
            {
                DSREF.GetPublishedDataSourceListRequest request = new DSREF.GetPublishedDataSourceListRequest(@event.SolutionId, @event.Token);
                DSREF.ListResultOf_PublishedDataSource result = client.GetPublishedDataSources(request);
                if (result.IsSuccess)
                {
                    @endEvent = new EndGetPublishedSolutionListEvent(NCLEventState.Finished, result.Data);
                }
                else
                {
                    @endEvent = new EndGetPublishedSolutionListEvent(NCLEventState.Failure, null);
                }
            }
            catch
            {
                @endEvent = new EndGetPublishedSolutionListEvent(NCLEventState.Failure, null);
            }
            NCLEventBus.Publish(@endEvent);
        }
        #endregion

        #region Get Item History
        /// <summary>
        /// 获取版本历史记录
        /// </summary>
        /// <param name="event"></param>
        [Coolzon.Common.Subscribe]
        public void OnReceiveBeginGetItemHistoryListEvent(BeginGetItemHistoryListEvent @event)
        {
            EndGetItemHistoryListEvent @endEvent = null;
            DSREF.DesignServiceClient client = Coolzon.ServiceProxy.DesignServiceProxyManager.Create();
            try
            {
                DSREF.GetItemHistoryListRequest request = new DSREF.GetItemHistoryListRequest(@event.ItemId);
                DSREF.ListResultOf_ItemHistory result = client.GetItemHistoryList(request);
                if (result.IsSuccess)
                {
                    @endEvent = new EndGetItemHistoryListEvent(NCLEventState.Finished, result.Data);
                }
                else
                {
                    @endEvent = new EndGetItemHistoryListEvent(NCLEventState.Failure, null);
                }
            }
            catch
            {
                @endEvent = new EndGetItemHistoryListEvent(NCLEventState.Failure, null);
            }
            NCLEventBus.Publish(@endEvent);
        }
        #endregion Get Item History

        #region Get Item History
        /// <summary>
        /// 获取版本历史记录
        /// </summary>
        /// <param name="event"></param>
        [Coolzon.Common.Subscribe]
        public void OnBeginGetLatestVersionEvent(BeginGetLatestVersionEvent @event)
        {
            EndGetLatestVersiontEvent @endEvent = null;
            DSREF.DesignServiceClient client = Coolzon.ServiceProxy.DesignServiceProxyManager.Create();
            try
            {
                DSREF.GetLatestVersionRequest request = new DSREF.GetLatestVersionRequest();
                request.Id = @event.Id;
                request.SolutionId = @event.SolutionId;
                request.Token = @event.Token;

                DSREF.GetLatestVersionResponse result = client.GetLatestVersion(request);
                if (result.IsSuccess)
                {
                    @endEvent = new EndGetLatestVersiontEvent(NCLEventState.Finished, result);
                }
                else
                {
                    @endEvent = new EndGetLatestVersiontEvent(NCLEventState.Failure, null);
                }
            }
            catch
            {
                @endEvent = new EndGetLatestVersiontEvent(NCLEventState.Failure, null);
            }
            NCLEventBus.Publish(@endEvent);
        }
        #endregion Get Item History
    }
}
